package com.ygqh.baby.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgProductActionGiftMapper;
import com.ygqh.baby.model.YgPromotionProductModel;
import com.ygqh.baby.po.YgProduct;
import com.ygqh.baby.po.YgProductActionGift;
import com.ygqh.baby.po.YgProductActionGiftExample;
import com.ygqh.baby.redis.RedisDao;
import com.ygqh.baby.service.YgProductActionGiftService;
import com.ygqh.baby.service.YgProductService;

@Service
public class YgProductActionGiftServiceImpl implements YgProductActionGiftService {

	@Autowired
	private YgProductActionGiftMapper ygProductActionGiftMapper;
	@Autowired
	private YgProductService ygProductService;
	@Autowired
	private RedisDao redisDao;

	private final static String redisKey = "Action_Product_Gift";

	@Override
	public List<YgProductActionGift> find() {
		YgProductActionGiftExample example = new YgProductActionGiftExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductActionGiftMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgProductActionGiftExample example = new YgProductActionGiftExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygProductActionGiftMapper.countByExample(example);
	}

	@Override
	public YgProductActionGift findById(Long id) {
		return ygProductActionGiftMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgProductActionGift ygProductActionGift) {
		return ygProductActionGiftMapper.insertSelective(ygProductActionGift);
	}

	@Override
	public int update(YgProductActionGift ygProductActionGift) {
		return ygProductActionGiftMapper.updateByPrimaryKeySelective(ygProductActionGift);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgProductActionGiftExample example = new YgProductActionGiftExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgProductActionGift record = new YgProductActionGift();
		record.setStatus(DataStatus.Delete);

		return ygProductActionGiftMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgProductActionGift ygProductActionGift) {
		int r = 0;
		if (ygProductActionGift.getId() != null) {
			r = this.update(ygProductActionGift);
		} else {
			r = this.save(ygProductActionGift);
		}
		setRedis();
		return r;
	}

	private void setRedis() {
		YgProductActionGiftExample example = new YgProductActionGiftExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		List<YgProductActionGift> list = ygProductActionGiftMapper.selectByExample(example);

		redisDao.set(redisKey, JSON.toJSONString(list), 0);

	}

	@Override
	public List<Long> getExitSingleGiftProductIds(Long[] productIds) {
		String res = redisDao.get(redisKey);
		List<YgProductActionGift> list = null;
		if (StringUtils.isNotBlank(res)) {
			list = JSON.parseArray(res, YgProductActionGift.class);
		} else {
			list = new ArrayList<YgProductActionGift>();
		}
		List<Long> result = null;

		if (!CollectionUtils.isEmpty(list)) {
			for (int i = 0; i < productIds.length; i++) {
				for (YgProductActionGift gift : list) {
					if (!gift.isValid()) {
						continue;
					}
					String[] pidStr = gift.getActionProductids().split(",");
					Long[] pIds = (Long[]) ConvertUtils.convert(pidStr, Long.class);
					List<Long> asList = Arrays.asList(pIds);
					if (asList.contains(productIds[i])) {
						if (result == null) {
							result = new ArrayList<Long>();
						}
						// 该商品参与活动
						String[] split = gift.getGiftProductids().split(",");
						Long[] priductIds = (Long[]) ConvertUtils.convert(split, Long.class);
						result.addAll(Arrays.asList(priductIds));
					}

				}

			}
		}
		return result;
	}

	@Override
	public List<YgPromotionProductModel> getSingleGiftList(Long[] productIds) {
		List<Long> ids = this.getExitSingleGiftProductIds(productIds);
		List<YgPromotionProductModel> giftList = null;
		if (!CollectionUtils.isEmpty(ids)) {
			giftList = ygProductActionGiftMapper.selectSingleGiftList(ids);
		}
		return giftList;
	}

	@Override
	public Boolean checkSingleGift(List<Long> productIds, Long skuId) {
		Long[] pIds = new Long[productIds.size()];

		pIds = productIds.toArray(pIds);
		List<Long> ids = this.getExitSingleGiftProductIds(pIds);
		Boolean isHas = isHasSkuBySingleGiftProduct(ids, skuId);

		return isHas;
	}

	private Boolean isHasSkuBySingleGiftProduct(List<Long> pIds, Long skuId) {
		if (CollectionUtils.isEmpty(pIds)) {
			return false;
		}

		return ygProductActionGiftMapper.isHasSkuInSingleGiftProduct(pIds, skuId);

	}

	@Override
	public List<YgProduct> findActionProduct(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultSet<YgProductActionGift> search(QueryInfo queryInfo, String q, Date startDate, Date endDate) {
		List<YgProductActionGift> list = ygProductActionGiftMapper.selectSuper(queryInfo, q, startDate, endDate);
		int count = ygProductActionGiftMapper.countSuper(q, startDate, endDate);
		ResultSet<YgProductActionGift> resultSet = new ResultSet<YgProductActionGift>(count, list);
		return resultSet;
	}

	@Override
	public List<YgProduct> findProductGift(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public YgProductActionGift getDetail(Long id) {

		return ygProductActionGiftMapper.findDetail(id);
	}

	@Override
	public List<YgProductActionGift> findActionGiftByGiftPId(Long pId) {
		return ygProductActionGiftMapper.selectActionGiftByGiftPId(pId);

	}

	@Override
	public BigDecimal getSalePriceOfActionProduct(Long productId) {
		List<YgProductActionGift> list = this.findActionGiftByGiftPId(productId);
		if (!CollectionUtils.isEmpty(list)) {

			String[] split = list.get(0).getActionProductids().split(",");
			YgProduct product = ygProductService.findById(Long.parseLong(split[0]));
			return product.getProductSalePrice();
		}
		return new BigDecimal(0);
	}

}
